package com.sensePlusVR.bean;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.utils.MyLog;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.ex.DbException;

import java.util.List;

@Table(name = "BannersDto")
public class BannersDto implements Parcelable{
	private static final String TAG = BannersDto.class.getSimpleName();
	@Column(name = "id", isId = true, autoGen = false)
	private Integer id;

	@Column(name = "title")
	private String title;

	@Column(name = "remark")
	private String remark;

	@Column(name = "coverUrl")
	private String coverUrl;

	@Column(name = "type")
	private Byte type;

	@Column(name = "targetType")
	private Byte targetType;

	@Column(name = "targetParam")
	private String targetParam;

	@Column(name = "startTime")
	private String startTime;

	@Column(name = "endTime")
	private String endTime;
	
	
	public BannersDto(){}
	
	public BannersDto(Parcel in){
		id = in.readInt();
		title = in.readString();
		remark = in.readString();
		coverUrl = in.readString();
		type = in.readByte();
		targetType = in.readByte();
		targetParam = in.readString();
		startTime = in.readString();
		endTime = in.readString();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getCoverUrl() {
		return coverUrl;
	}

	public void setCoverUrl(String coverUrl) {
		this.coverUrl = coverUrl;
	}

	public Byte getType() {
		return type;
	}

	public void setType(Byte type) {
		this.type = type;
	}

	public Byte getTargetType() {
		return targetType;
	}

	public void setTargetType(Byte targetType) {
		this.targetType = targetType;
	}

	public String getTargetParam() {
		return targetParam;
	}

	public void setTargetParam(String targetParam) {
		this.targetParam = targetParam;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	@Override
	public String toString() {
		return "BannersDto [id=" + id + ", title=" + title + ", remark="
				+ remark + ", coverUrl=" + coverUrl + ", type=" + type
				+ ", targetType=" + targetType + ", targetParam=" + targetParam
				+ ", startTime=" + startTime + ", endTime=" + endTime + "]";
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeInt(id);
		out.writeString(title);
		out.writeString(remark);
		out.writeString(coverUrl);
		out.writeByte(type);
		out.writeByte(targetType);
		out.writeString(targetParam);
		out.writeString(startTime);
		out.writeString(endTime);
	}
	
	public static final Creator<BannersDto> CREATOR = new Creator<BannersDto>() {

		@Override
		public BannersDto createFromParcel(Parcel in) {
			return new BannersDto(in);
		}

		@Override
		public BannersDto[] newArray(int size) {
			return new BannersDto[size];
		}
	};
	
	public static void saveOrUpdate(List<BannersDto> bannersDto) {
		MyLog.d(TAG, "saveOrUpdate bannersDto : " + bannersDto.toString());
		try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
    			DbManager db = VRApplication.getDbManager();
    			db.saveOrUpdate(bannersDto);
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "bannersDto update failed: "+e);
    	}
	}
	
	public static List<BannersDto> getBannerList(){
		Context context = VRApplication.getAppContext();
		if(context != null){
			DbManager db = VRApplication.getDbManager();
			try {
				return db.selector(BannersDto.class).findAll();
			} catch (DbException e) {
				MyLog.e(TAG, "getBannerList failed: "+e);
				return null;
			}
		}
		return null;
	}
	
	public static void delAllData(){
		try {
			Context context = VRApplication.getAppContext();
			if (context != null) {
				DbManager db = VRApplication.getDbManager();
				db.delete(BannersDto.class);
			}
		} catch (Exception e) {
			MyLog.e(TAG, "delAllData failed: " + e);
		}
		
	}
	
}
